<%@ page import="java.util.GregorianCalendar" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="java.util.Date" %>
<%--
  Created by IntelliJ IDEA.
  User: a8945
  Date: 2019-07-11
  Time: 20:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<%
    // 获取session创建时间
    Date createTime = new Date(session.getCreationTime());
    // 获取最后访问页面的时间
    Date lastAccessTime = new Date(session.getLastAccessedTime());

    String title = "再次访问菜鸟教程实例";
    Integer visitCount = new Integer(0);
    String visitCountKey = new String("visitCount");
    String userIDKey = new String("userID");
    String userID = new String("ABCD");

    // 检测网页是否有新的访问用户
    if (session.isNew()){
        title = "访问菜鸟教程实例";
        session.setAttribute(userIDKey, userID);
        session.setAttribute(visitCountKey,  visitCount);
    } else {
        visitCount = (Integer)session.getAttribute(visitCountKey);
        visitCount += 1;
        userID = (String)session.getAttribute(userIDKey);
        session.setAttribute(visitCountKey,  visitCount);
    }
%>
<!DOCTYPE html>
<head>
    <title>Index.jsp</title>
    <script type="text/javascript" src="resources/js/jquery-3.4.1.min.js"></script>
</head>
<body>
<h2>自动刷新实例</h2>
<%= request.getParameter("id")%>

message = ${message}<br>
<%
    out.println("Your IP address is:"+request.getRemoteAddr());
%>
<br>
今天的日期是<%=(new java.util.Date()).toString()%><br>
<!-- if-else实例 -->
<%! int day=1; %>
<% if(day == 6 || day == 7){ %>
    <p>今天是周末</p>
<% }else{ %>
    <p>今天是工作日</p>
<% } %>


<h1>Session 跟踪</h1>

<table border="1" align="center">
    <tr bgcolor="#949494">
        <th>Session 信息</th>
        <th>值</th>
    </tr>
    <tr>
        <td>id</td>
        <td><% out.print( session.getId()); %></td>
    </tr>
    <tr>
        <td>创建时间</td>
        <td><% out.print(createTime); %></td>
    </tr>
    <tr>
        <td>最后访问时间</td>
        <td><% out.print(lastAccessTime); %></td>
    </tr>
    <tr>
        <td>用户 ID</td>
        <td><% out.print(userID); %></td>
    </tr>
    <tr>
        <td>访问次数</td>
        <td><% out.print(visitCount); %></td>
    </tr>
</table>
<!-- 获取值的时候，对应的是addAttribute的第一个参数！取了个别名为c-->
<c:forEach items="${USER}" var="c">
    <tr>
        <td>id:${c.id}</td>
        <td>username:${c.userName}</td>
        <td>age:${c.age}</td>
    </tr>
</c:forEach>
</body>
</html>
